define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Wanttoagent, GoPage, $timeout, $ionicSlideBoxDelegate, GoAddress, GoDialog, GoCatTypeAuth, Auth, $sce) {
		
		//画面加载之后
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			GoPage.init_top($scope, 44, 0);
		}, false);

		//滑动切换
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		

		$scope.init_data = function() {
			Wanttoagent.getlist(10, 1, function(data) {
				$scope.list = data;
			});
		}
		$scope.init_data(); //必须在定义之后

		//数据模型
		$scope.input = new Object();
		$scope.initialize = function() { //初始化数据
			$scope.input.info_id = "";
			$scope.input.cat_type_id = ""; //分类
			$scope.input.region_id = ""; //地区
			$scope.input.province = null; //省市区三级联动
			$scope.input.city = null;
			$scope.input.district = null;
			$scope.input.cat1 = null;
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
			$scope.input.phone = "";
			$scope.input.capital = "";
			$scope.input.reason = "";
		}
		$scope.initialize(); //发布成功后，要初始化所有数据，故将所有参数写在方法体里

		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data) {
			$scope.input.city = null;
			$scope.input.district = null;
		};
		$scope.listen_city = function(data) {
			$scope.input.district = null;
		};
		//定义绑定
		var items = new Array();
		var obj1 = {
			name: 'input.province'
		};
		var obj2 = {
			name: 'input.city'
		};
		var obj3 = {
			name: 'input.district'
		};
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		/***分类三级联动***/
		//下拉侦听
		$scope.listen_cat1 = function(data, obj) {
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
		};
		$scope.listen_cat2 = function(data, obj) {
			$scope.input.cat3 = null;
		};
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = {
			name: 'input.cat1'
		};
		var obj2_cat = {
			name: 'input.cat2'
		};
		var obj3_cat = {
			name: 'input.cat3'
		};
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);

		//发布代理或修改
		$scope.add = function() {
			//分类
			if($scope.input.cat3) {
				$scope.input.cat_type_id = $scope.input.cat1.name + "/" + $scope.input.cat2.name + "/" + $scope.input.cat3.name + ":" + $scope.input.cat3.id;
			} else if($scope.input.cat2) {
				$scope.input.cat_type_id = $scope.input.cat1.name + "/" + $scope.input.cat2.name + ":" + $scope.input.cat2.id;
			} else if($scope.input.cat1) {
				$scope.input.cat_type_id = $scope.input.cat1.name + ":" + $scope.input.cat1.id;
			}
			//地域
			if($scope.input.district) {
				$scope.input.region_id = $scope.input.province.name + "/" + $scope.input.city.name + "/" + $scope.input.district.name + ":" + $scope.input.district.id;
			} else if($scope.input.city) {
				$scope.input.region_id = $scope.input.province.name + "/" + $scope.input.city.name + ":" + $scope.input.city.id;
			} else if($scope.input.province) {
				$scope.input.region_id = $scope.input.province.name + ":" + $scope.input.province.id;
			}

			if($scope.input.cat_type_id == "") {
				GoDialog.toast("请选择品类！");
			} else if($scope.input.region_id == "") {
				GoDialog.toast("请选择代理区域！");
			} else if(!$scope.input.reason) {
				GoDialog.toast("请输入代理描述！");
			} else if(!$scope.input.capital) {
				GoDialog.toast("请输入投入资金！");
			} else {
				if(isNaN($scope.input.capital)){
					GoDialog.toast("输入的投入资金不正确！");
					return;
				}
				Wanttoagent.beagenter_edit($scope.input.info_id, $scope.input.cat_type_id, $scope.input.region_id, $scope.input.reason, $scope.input.capital, $scope.input.phone, function(data) {
					if(data.success) {
						GoDialog.toast("提交成功！");
						$scope.activeTab(0); //提交成功返回“已发布的代理”
						$scope.init_data(); //发布成功，要刷新数据
						$scope.initialize(); //重置已经发布过的数据
					} else {
						GoDialog.toast(data.msg);
					}
				});
			}
		}

		//根据id查找
		$scope.findById_edit = function(info_id) {
			Wanttoagent.findById(info_id, function(data) {
				console.log(data);
				$scope.detailById = data;
				$scope.input.info_id = info_id;
				$scope.input.phone = parseInt($scope.detailById.phone) ;
				$scope.input.capital = parseFloat($scope.detailById.money);
				$scope.input.reason = $scope.detailById.requirement;
				$scope.activeTab(1);
				//先清空
				$scope.input.province = null;
				$scope.input.city = null;
				$scope.input.district = null;
				//地区初始化
				GoAddress.init_region($scope, data.region_id, function(val1, val2, val3) {
					//使用定时器来指引
					$scope.isUnwatch = true; //不进行侦听操作
					$scope.input.province = val1;
					$scope.input.city = val2;
					$scope.input.district = val3;
					$timeout(function() {
						$scope.isUnwatch = false; //不进行侦听操作
					}, 1000);
				});
				//分类初始化
				GoCatTypeAuth.inti_cat($scope, data.cat_type_id, function(val1, val2, val3) {
					//使用定时器来指引
					$scope.isUnwatch = true; //不进行侦听操作
					$scope.input.cat1 = val1;
					$scope.input.cat2 = val2;
					$scope.input.cat3 = val3;
					$timeout(function() {
						$scope.isUnwatch = false; //不进行侦听操作
					}, 1000);
				});
			});
		}

		//删除
		$scope.delete = function(info_id, index) {
			Wanttoagent.delete_info_id(info_id, function(data) {
				console.log(data)
				if(data.success) {
					GoDialog.toast("删除成功！");
					$scope.list.beagenter.splice(index, 1);
				} else {
					GoDialog.toast(data.msg);
				}
			});
		}

		//下拉刷新
		$scope.input.nPage = 1;
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Wanttoagent.getlist(10, 1, function(data) {
					if(data != null) {
						$scope.list = data;
					} else {
						$scope.list = new Object();
					}
					$scope.input.nPage = 1;
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Wanttoagent.getlist(10, $scope.input.nPage + 1, function(data) {
					console.log(data);
					if(data != null) {
						if($scope.list.beagenter != undefined) {
							$scope.list.beagenter = $scope.list.beagenter.concat(data.beagenter);
							$scope.input.nPage += 1;
						} else {
							$scope.list = data;
						}
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};

		//地域id
		$scope.regionid = function() {
			if($scope.input.province == null) {
				$scope.input.region_id = "";
			} else if($scope.input.city == null) {
				$scope.input.region_id = $scope.input.province.id;
			} else if($scope.input.district == null) {
				$scope.input.region_id = $scope.input.city.id;
			} else {
				$scope.input.region_id = $scope.input.district.id;
			}
		}

	}

	controller.$inject = ['$scope', 'Wanttoagent', 'GoPage', '$timeout', '$ionicSlideBoxDelegate', 'GoAddress', 'GoDialog', 'GoCatTypeAuth', 'Auth','$sce'];

	return app.registerController("wanttoagentctrl",controller);
});